package util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

public class DBUtilEx {

	private static BasicDataSource bs;

	static {
		Properties p = new Properties();
		try {
			p.load(DBUtilEx.class.getClassLoader().
					getResourceAsStream("db.properties"));
			String driver = p.getProperty("driver");
			String user = p.getProperty("user");
			String url = p.getProperty("url");
			String pwd = p.getProperty("pwd");
			String initsize = p.getProperty("initsize");
			String maxsize = p.getProperty("maxsize");

			bs = new BasicDataSource();
			bs.setDriverClassName(driver);
			bs.setUrl(url);
			bs.setUsername(user);
			bs.setPassword(pwd);
			bs.setInitialSize(Integer.parseInt(initsize));
			bs.setMaxActive(Integer.parseInt(maxsize));

		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	
	public static Connection getConnection() throws SQLException{
		return bs.getConnection();
		
	}
	
	
	public static void close(Connection conn){
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException("归还连接失败",e);
			}
			
			
		}
		
	}
	
	
	

}
